import ConfirmVue from './confirm'
import {h, render} from 'vue'

export default (
    {
      message,
      title,
      confirmBtnText,
      cancelBtnText,
      onCancel,
      onConfirm,
      onClose
    }
) => new Promise((resolve, reject) => {
  const vnode = h(ConfirmVue, {
    message,
    title,
    confirmBtnText,
    cancelBtnText,
    onCancel: (v) => {
      onCancel && onCancel()
      let timer = setTimeout(() => {
        vnode.el?.remove()
        onClose && onClose()
        clearTimeout(timer)
        timer = null
      }, 410)
      reject(v)
    },
    onConfirm: (v) => {
      onConfirm && onConfirm()
      let timer = setTimeout(() => {
        vnode.el?.remove()
        clearTimeout(timer)
        onClose && onClose()
        timer = null
      }, 410)
      resolve(v)
    },
  })
  const container = document.createDocumentFragment()
  render(vnode, container)
  document.body.appendChild(container)
})